package MyProject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;

public class Project {

    private static final ArrayList<Integer> money = new ArrayList<>();
    private static final ArrayList<MyThread> sort = new ArrayList<>();
    public static void playGame() throws InterruptedException {
        //先创建好红包
        createRedPackets();

        //创建线程来抢红包
        for (int i = 0; i < 100; i++) {
            MyThread myThread = new MyThread(money,(i+1) + " 号员工");
            sort.add(myThread);
            myThread.start();

        }

        for (MyThread myThread : sort) {
            myThread.join();
        }

        System.out.println("=============红包排序==============");
        sort.stream().sorted((o1, o2) -> (o2.getSum() - o1.getSum())).forEach(p->{
            System.out.println(p.getName() + "抢到红包总计: " + p.getSum());
        });
    }

    private static void createRedPackets(){
        Random random = new Random();
        for (int i = 0; i < 160; i++) {
            money.add(random.nextInt(30) + 1);
        }

        for (int i = 0; i < 40; i++) {
            money.add(random.nextInt(70)+31);
        }

        //打乱顺序
        Collections.shuffle(money);
        System.out.println(money);
    }

}
